package com.yonyou.pmclouds.plan.rmiimpl;

import com.yonyou.pmclouds.basecom.exception.BusinessException;
import com.yonyou.pmclouds.plan.entity.ScheduleItemVO;
import com.yonyou.pmclouds.plan.entity.ScheduleVO;
import com.yonyou.pmclouds.plan.rmiitf.IScheduleHandleService;
import com.yonyou.pmclouds.plan.rmiitf.IScheduleItemProcessor;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @Description: 计划任务项迭代处理类
 * @Author: zhangzhonghao
 * @Date: 2020-03-06
 */

@Service
public class ScheduleHandleServiceImpl implements IScheduleHandleService {

    private List<IScheduleItemProcessor> iteratorBeforeProcessorList = new ArrayList<>();

    private List<IScheduleItemProcessor> iteratorAfterProcessorList = new ArrayList<>();

    @Override
    public void handle(ScheduleVO scheduleVO, ScheduleVO origScheduleVO) throws BusinessException {

        ScheduleItemVO[] scheduleItemVOS = scheduleVO.getScheduleItemVOS();
        if (scheduleItemVOS == null || scheduleItemVOS.length <= 0) {
            throw new BusinessException("计划明细项不允许为空！");
        }


        for (ScheduleItemVO scheduleItemVO : scheduleItemVOS) {
            iteratorHandle(scheduleVO, scheduleItemVO, null, scheduleItemVO);
        }

        init();
    }

    @Override
    public void addIteratorBeforeProcessor(IScheduleItemProcessor itemProcessor) {
        iteratorBeforeProcessorList.add(itemProcessor);
    }

    @Override
    public void addIteratorAfterProcessor(IScheduleItemProcessor itemProcessor) {
        iteratorAfterProcessorList.add(itemProcessor);
    }

    private void iteratorHandle(ScheduleVO scheduleVO, ScheduleItemVO rootParentItemVO, ScheduleItemVO parentItemVO,
                                ScheduleItemVO childItemVO) throws BusinessException {

        handleProcessorList(iteratorBeforeProcessorList, scheduleVO, rootParentItemVO, parentItemVO, childItemVO);

        if(childItemVO.getChildren() != null &&childItemVO.getChildren().length > 0){

            for(ScheduleItemVO childItemVOTemp : childItemVO.getChildren()){
                iteratorHandle(scheduleVO, rootParentItemVO, childItemVO, childItemVOTemp);
            }

        }

        handleProcessorList(iteratorAfterProcessorList, scheduleVO, rootParentItemVO, parentItemVO, childItemVO);
    }

    private void handleProcessorList(List<IScheduleItemProcessor> itemProcessors, ScheduleVO scheduleVO,
                                     ScheduleItemVO rootParentItemVO, ScheduleItemVO parentItemVO,
                                     ScheduleItemVO childItemVO) throws BusinessException {
        if(itemProcessors.size() <= 0){
            return;
        }
        for (IScheduleItemProcessor itemProcessor : itemProcessors) {
            itemProcessor.process(scheduleVO, rootParentItemVO, parentItemVO, childItemVO);
        }
    }

    private void init(){
        iteratorAfterProcessorList = new ArrayList<>();
        iteratorBeforeProcessorList = new ArrayList<>();
    }

}
